package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.sun.deploy.util.StringUtils;
import net.zjitc.entity.Role;
import net.zjitc.entity.Vo.RolePermissionVo;
import net.zjitc.mapper.PermissionMapper;
import net.zjitc.mapper.PermissionRoleMapper;
import net.zjitc.mapper.RoleMapper;
import net.zjitc.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class IRoleService implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionRoleMapper permissionMapper;

    @Override
    public List<Role> getRole() {
        List<Role> roles = roleMapper.selectList(null);
        for (Role role:roles) {
            List<RolePermissionVo> oneList=new ArrayList<>();
            List<RolePermissionVo> twolist=new ArrayList<>();
            List<RolePermissionVo> thireelist=new ArrayList<>();
            String psIds =role.getPs_ids();
            String[] psId = psIds.split(",");
            for (String sid:psId) {
                int id=Integer.parseInt(sid.equals("")?"0":sid);
                RolePermissionVo permission = permissionMapper.selectById(id);
                if (permission==null){
                    continue;
                }
                if (permission.getPs_level().equals("0")){
                    oneList.add(permission);
                }else if (permission.getPs_level().equals("1")){
                    twolist.add(permission);
                }else if (permission.getPs_level().equals("2")){
                    thireelist.add(permission);
                }
            }
            getThreeleveList(oneList, twolist, thireelist);
            role.setChildren(oneList);
        }
        return roles;
    }

    @Override
    public Role addRole(Role role) {
        if (role.getPs_ids()==null){
            role.setPs_ids("0");
        }
        int insert = roleMapper.insert(role);
        return role;
    }

    @Override
    public Role updateRole(Role role, int id) {
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",id);
        roleMapper.update(role, wrapper);
        Role role1 = roleMapper.selectById(id);
        return role1;
    }

    @Override
    public int delete(int id) {
        int i = roleMapper.deleteById(id);
        return i;
    }

    @Override
    public int addrids(int id, String isStr) {
        Role role = roleMapper.selectById(id);
        role.setPs_ids(isStr);
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",id);
        int i = roleMapper.update(role, wrapper);
        return i;
    }

    @Override
    public Role deleteRightId(int id, int rightid) {
        Role role = roleMapper.selectById(id);
        String[] ps_rids = role.getPs_ids().split(",");
        List<String> rids=new ArrayList<>();
        for (int i = 0; i < ps_rids.length; i++) {
            if (Integer.parseInt(ps_rids[i])!=rightid){
                rids.add(ps_rids[i]);
            }
        }
        String rid = StringUtils.join(rids, ",");
        role.setPs_ids(rid);
        roleMapper.updateById(role);
        //查询role
        List<RolePermissionVo> oneList=new ArrayList<>();
        List<RolePermissionVo> twolist=new ArrayList<>();
        List<RolePermissionVo> thireelist=new ArrayList<>();
        String psIds =role.getPs_ids();
        String[] psId = psIds.split(",");
        for (String sid:psId) {
            int pid=Integer.parseInt(sid.equals("")?"0":sid);
            RolePermissionVo permission = permissionMapper.selectById(pid);
            if (permission==null){
                continue;
            }
            if (permission.getPs_level().equals("0")){
                oneList.add(permission);
            }else if (permission.getPs_level().equals("1")){
                twolist.add(permission);
            }else if (permission.getPs_level().equals("2")){
                thireelist.add(permission);
            }
        }
        getThreeleveList(oneList, twolist, thireelist);
        role.setChildren(oneList);

        return role;
    }

    @Override
    public Role findRoleById(int id) {
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",id);
        wrapper.select("role_id as id,role_name as roleName,role_desc as roleDesc");
        Role role = roleMapper.selectOne(wrapper);
        return role;
    }

    private void getThreeleveList(List<RolePermissionVo> oneList, List<RolePermissionVo> twolist, List<RolePermissionVo> thireelist) {
        nodeCateToParentPermission(thireelist,twolist);
        nodeCateToParentPermission(twolist,oneList);
    }

    //查找父级 并添加
    public void nodeCateToParentPermission(List<RolePermissionVo> nodeList,List<RolePermissionVo> parentList){
        for (RolePermissionVo parent:parentList){
            List<RolePermissionVo> temp = new ArrayList<>();
            for (RolePermissionVo node:nodeList){
                if (node.getPs_pid().intValue()==parent.getId().intValue()){
                    temp.add(node);
                }
            }
            parent.setChildren(temp);
        }
    }


}

